﻿import {
    Component,
    OnInit,
    Injector,
    ViewEncapsulation,
    ElementRef,
    ViewChild,
    Input,
    Output,
    EventEmitter
} from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { BatchServiceProxy } from '@shared/service-proxies/service-proxies';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { ArrayToTreeConverterService } from '@shared/utils/array-to-tree-converter.service';
import { MenuItem, TreeNode } from 'primeng/api';

@Component({
    selector: 'assem-tree',
    templateUrl: './assem-tree.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class AssemTreeComponent extends AppComponentBase implements OnInit {
    _batch; // {id: 'xxx', quantity: 0}
    @Input() set batch(val) {
        this._batch = val;
        this.loadTree();
    }

    @Output() getOwnCosts = new EventEmitter<TreeNode>();
    @Output() getOwnAndChildrenCosts = new EventEmitter<TreeNode>();

    constructor(
        injector: Injector,
        private _batchService: BatchServiceProxy,
        private _arrayToTreeConverterService: ArrayToTreeConverterService
    ) {
        super(injector);
    }

    assemContextMenuItems: MenuItem[];

    treeData;

    treeMappings;

    selectedAssem: TreeNode;

    ngOnInit(): void {
        this.treeMappings = [
            {
                target: 'label',
                targetFunction(item) {
                    return item.partId + '@' + item.partQuantity;
                }
            },
            {
                target: 'title',
                targetFunction(item) {
                    return item.taskNum + '(' + item.productId + ')' + item.productName;
                }
            },
            {
                target: 'code',
                targetFunction(item) {
                    return item.partId;
                }
            },
            {
                target: 'expanded',
                targetFunction(item) {
                    return true;
                }
            },
            {
                target: 'expandedIcon',
                value: 'fa fa-cog m--font-warning'
            },
            {
                target: 'collapsedIcon',
                value: 'fa fa-cog m--font-warning'
            },
            {
                target: 'selectable',
                value: true
            }
        ];

        this.assemContextMenuItems = this.getContextMenuItems();
    }

    loadTree() {
        if (!this._batch || !this._batch.id) {
            return;
        }
        this._batchService.getFlatAssemRels(this._batch.quantity, true, this._batch.id).subscribe(result => {

            console.log(result)

            this.treeData = this._arrayToTreeConverterService.createTree(
                result.items,
                'assemId',
                'partId',
                null,
                'children',
                this.treeMappings
            );
        });
    }

    onNodeSelect(event) {}

    private getContextMenuItems(): any[] {
        const canManageOrganizationTree = this.isGranted(
            'Pages.Administration.OrganizationUnits.ManageOrganizationTree'
        );

        let items = [
            {
                label: '查看成本',
                disabled: false,
                command: event => {
                    this.getOwnCosts.emit(this.selectedAssem);
                }
            },
            {
                label: '查看成本(包含子项)',
                disabled: false,
                command: () => {
                    this.getOwnAndChildrenCosts.emit(this.selectedAssem);
                }
            }
        ];

        return items;
    }
}
